<?php
include_once 'news_class.php';
include_once ('db_manage.php');

function getArray($sql) {
	$array = array ();
	$result = mysql_query ( $sql );
	$i = 0;
	while ( ($rs = mysql_fetch_object ( $result )) != false ) {
		$news = new News ();
		
		$news->setId ( $rs->id );
		$news->setTitle ( $rs->title );
		$news->setContent ( $rs->content );
		$news->setFacepic ( $rs->facepic );
		$news->setSmallpic ( $rs->smallpic );
		$news->setPostuser ( $rs->postuser );
		$news->setPostname ( $rs->postname );
		$news->setPostdate ( $rs->postdate );
		$news->setStatus ( $rs->status );
		$news->setHot ( $rs->hot );
		$news->setReason ( $rs->reason );
		$news->setCreateTime ( $rs->createTime );
		//		$news = array();
		//		$nees['id'] = $rs->id;
		//		$nees['title'] = $rs->title;
		//		$nees['content'] = $rs->content;
		//		$nees['facepic'] = $rs->facepic;
		//		$nees['smallpic'] = $rs->smallpic;
		//		$nees['postuser'] = $rs->postuser;
		//		$nees['postname'] = $rs->postname;
		//		$nees['postdate'] = $rs->postdate;
		//		$nees['status'] = $rs->status;
		$array [$i] = $news;
		$i ++;
	}
	mysql_free_result ( $result );
	return $array;
}

function update($sql) {
	if (mysql_query ( $sql )) {
		return mysql_insert_id ();
	}
}

function updateHot($id) {
	$sql = "update pcd_news set hot = hot + 1 where id = " . $id;
	mysql_query ( $sql );
}

function getTopNewsApprove($count) {
	$sql = "select * from pcd_news where status = 1 order by id desc limit 0," . $count;
	return getArray ( $sql );
}

function getTotalNewsCountApprove() {
	$sql = "select count(*) as total from pcd_news where status = 1";
	$result = mysql_query ( $sql );
	while ( ($rs = mysql_fetch_object ( $result )) != false ) {
		$total = $rs->total;
	}
	mysql_free_result ( $result );
	return $total;
}

function getNewsByPageApprove($page, $num_row_of_page) {
	$start = $page * $num_row_of_page;
	$sql = "select * from pcd_news where status = 1 order by postdate desc limit " . $start . ", " . $num_row_of_page;
	return getArray ( $sql );
}

function getTopNews($count) {
	$sql = "select * from pcd_news order by id desc limit 0," . $count;
	return getArray ( $sql );
}

function getTotalNewsCount() {
	$sql = "select count(*) as total from pcd_news";
	$result = mysql_query ( $sql );
	while ( ($rs = mysql_fetch_object ( $result )) != false ) {
		$total = $rs->total;
	}
	mysql_free_result ( $result );
	return $total;
}

function getNewsByPage($page, $num_row_of_page) {
	$start = $page * $num_row_of_page;
	$sql = "select * from pcd_news order by postdate desc limit " . $start . ", " . $num_row_of_page;
	return getArray ( $sql );
}

function getNewsById($id) {
	$sql = "select * from pcd_news where id =" . $id;
	$array = getArray ( $sql );
	if (count ( $array ) > 0) {
		return $array [0];
	}
}

function createNews($news) {
	$sql = "insert into pcd_news(title,content,facepic,smallpic,postuser,postname,postdate,status,createTime) values('" . $news ['title'] . "','" . $news ['content'] . "','" . $news ['facepic'] . "','" . $news ['smallpic'] . "'," . $news ['postuser'] . ",'" . $news ['postname'] . "',now()," . $news ['status'] . ",now())";
	return update ( $sql );
}

function updateNews($news) {
	$sql = "UPDATE pcd_news SET title = '" . $news ['title'] . "',content = '" . $news ['content'] . "',facepic = '" . $news ['facepic'] . "',smallpic='" . $news ['smallpic'] . "' WHERE id = " . $news ['id'];
	if ($news ['facepic'] == "") {
		$sql = "UPDATE pcd_news SET title = '" . $news ['title'] . "',content = '" . $news ['content'] . "' WHERE id = " . $news ['id'];
	}
	//	echo $sql;
	update ( $sql );
}

function approveNews($newsID) {
	$sql = "update pcd_news set status = 1";
	update ( $sql );
}

?>